package com.uvt.agenda;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CalendarView;
import android.widget.Toast;
import com.sleepbot.datetimepicker.time.RadialPickerLayout;
import com.sleepbot.datetimepicker.time.TimePickerDialog;
import com.uvt.agenda.commands.CommandsManager;
import com.uvt.agenda.commands.GeneratePDFDocument;
import com.uvt.agenda.database.DatabaseManager;
import com.uvt.agenda.pdf.Builder;
import com.uvt.agenda.pdf.PdfBuilder;
import com.uvt.agenda.pdf.PdfCalendar;
import com.uvt.agenda.view.DatePickerDialog;
import com.uvt.agenda.view.DialogResultListener;
import com.uvt.agenda.view.NewEventView;
import org.joda.time.DateTime;
import android.content.Context;

import java.util.ArrayList;
import java.util.Random;

public class AndroidAgenda extends Activity {

    CalendarView cal;
    public static Context context;


    DatabaseManager dbManager;
    Random rand = new Random();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dbManager = new DatabaseManager(this);
        dbManager.openDataBase();

        findViewById(R.id.create_pdf).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    GeneratePDFDocument generatePDFDocument = new GeneratePDFDocument(AndroidAgenda.context,2014);
                    CommandsManager.executeCommand(generatePDFDocument);

            }
        });

        findViewById(R.id.add_event).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = rand.nextInt(10);
                Event event = new Event("Test Event", "Test description", DateTime.now().plusDays(i).getMillis(), DateTime.now().plusDays(i + 1).getMillis());
                dbManager.saveNewEvent(event);
            }
        });

        findViewById(R.id.get_all_events).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Event> events = dbManager.getAllEvents();
                ArrayList<Event> monthEvents = dbManager.getMonthEvents(DateTime.now().withDate(2014,2,1).withTimeAtStartOfDay());

            }
        });

        findViewById(R.id.new_event_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewEventView newEventView = new NewEventView(AndroidAgenda.this);
                newEventView.show();
            }
        });

        findViewById(R.id.date_picker).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(AndroidAgenda.this);
                datePickerDialog.show();
                datePickerDialog.setResultListener(new DialogResultListener() {
                    @Override
                    public void onDoneClick(Object result) {
                        if (result instanceof DateTime) {
                            DateTime dt = (DateTime) result;
                            Toast.makeText(AndroidAgenda.this, "Selected: " + dt.getYear() + " "  + dt.getMonthOfYear() + " " + dt.getDayOfMonth(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelClick() {

                    }
                });
            }
        });

        findViewById(R.id.time_picker).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
                        Toast.makeText(AndroidAgenda.this, "Time: " + hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
                    }
                };
//                final TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(getApplicationContext(), listener, 12, 22, false, false);
                TimePickerDialog timePickerDialog = new TimePickerDialog(AndroidAgenda.this);
                timePickerDialog.show();
                timePickerDialog.setOnTimeSetListener(listener);
            }
        });
    }

}